<template>
<div class="articaldetail">
    <div class="header">
        <div class="left">
          <!-- $router.back() 路由返回上一个路由路径的方法 -->
            <van-icon name="arrow-left back"
                      @click="$router.back()" />
            <span class="iconfont iconnew new"></span>
        </div>
        <span :class="{active: article.has_follow}"
         @click="followFn">{{article.has_follow?'已关注':'关注'}}</span>
    </div>
    <div class="detail">
        <div class="title">{{  article.title }}</div>
        <div class="desc">
            <!-- 
                当浏览器打开页面的时候，DOM会立刻进行渲染，此刻渲染的时候
                数据是没有请求回来的，因此在渲染的时候article.user拿到的是undefined，
                那么相当于undefined.nickname就是直接报错了（js没有这样的语法的所以报错）
             -->
            <span>{{ article.user&&article.user.nickname }}</span> &nbsp;&nbsp;
            <span>{{ article.create_date }}</span>
        </div>
        <div v-if="article.type==1" class="content" v-html="article.content"></div>
        <div v-if="article.type==2" class="content">
          <!-- poster: 视频封面 controls: 视频控制器 -->
          <video :poster="article.cover&&article.cover[0].url|joinPath" controls :src="article.content" ></video>
        </div>
        <div class="opt">
            <span class="like">
                <van-icon name="good-job-o" />点赞
            </span>
            <span class="chat">
                <van-icon name="chat"
                          class="w" />微信
            </span>
        </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
        <h2>精彩跟帖</h2>
        <div class="item">
            <div class="head">
                <img src=""
                     alt />
                <div>
                    <p>火星网友</p>
                    <span>2小时前</span>
                </div>
                <span>回复</span>
            </div>
            <div class="text">文章说得很有道理</div>
        </div>
        <div class="more">更多跟帖</div>
    </div>
    <!-- 底部评论块 -->
    <footerComment></footerComment>
</div>
</template>

<script>
import footerComment from "@/components/footerComment.vue"
import { articleDetail } from "@/api/article.js"
import { follow, unfollow } from "@/api/user.js"
export default {
    components: {
        footerComment
    },
    data(){
        return {
            article: {}
        }
    },
    created(){
        let id = this.$route.query.id;
        articleDetail(id).then(res=>{
            // console.log(94, res);
            this.article = res.data.data;
        })
    },
    methods: {
      // 关注/取消关注 用户
      followFn(){
        // this.article.has_follow当这个值为true，表示要取消关注
        // 否则进行关注
        // if(this.article.has_follow){
        //   // 取消关注
        //   unfollow(this.article.user.id).then(res=>{
        //     // console.log(85, res);
        //     if(res.data.message == "取消关注成功"){
        //       this.article.has_follow = !this.article.has_follow;
        //       this.$toast.success(res.data.message)
        //       return
        //     }
        //     this.$toast.fail(res.data.message)
        //   })
        // }else{
        //   // 关注
        //   follow(this.article.user.id).then(res=>{
        //     if(res.data.message === "关注成功"){
        //       this.article.has_follow = !this.article.has_follow;
        //       this.$toast.success(res.data.message)
        //       return
        //     }
        //     this.$toast.fail(res.data.message)
        //   })
        // }
        // 代码整合写法
        let api = this.article.has_follow?unfollow:follow;
        api(this.article.user.id).then(res=>{
            if(res.data.message === "关注成功"||res.data.message == "取消关注成功"){
              this.article.has_follow = !this.article.has_follow;
              this.$toast.success(res.data.message)
              return
            }
            
            // this.$toast.fail(res.data.message)
          })
        }
    }
}
</script>

<style lang="less" scoped>
.articaldetail{
    padding-bottom: 52px;
}
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    &.active {
      background-color: #fff;
      color: #000;
      border: 1px solid #000;
    }
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
//   通过v-html渲染的模板，vue解析的时候也把他当做组件进行渲染
// 因此设置模板里面标签样式的时候，也可以使用/deep/样式穿透来设置其样式
  /deep/.content {
    // text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    img{
        width: 100%;
    }
    video{
      width: 100%;
    }
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: (50/360) * 100vw;
        height: (50/360) * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>